package controllers

import (
	//"container/list"
	"time"

	//"github.com/astaxie/beego"
	"github.com/gorilla/websocket"
	//"encoding/json"
)

type EventType int

const (
	EVENT_JOIN = iota
	EVENT_LEAVE
	EVENT_MESSAGE
)


type Client struct {
	Type      EventType // JOIN, LEAVE, MESSAGE
	User      string  //用户名
	Timestamp int // Unix timestamp (secs)
	Content   string
	Conn *websocket.Conn
}

var (
	messageChan = make(chan Client ,10)
)

func newClient(ep EventType, user, msg string, ws *websocket.Conn) Client {
	return Client{ep, user, int(time.Now().Unix()), msg,ws}
}

func exMessage()  {
	for {
		select {
			case event:= <- messageChan:
				broadcastWebSocket(event)
		}
	}
}

func init()  {
	go exMessage()
}